package graphics;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;


public class Canvas extends JComponent
{
    private Animatable myModel;

    public Canvas (java.awt.Dimension size)
    {
        setPreferredSize(size);
        setFocusable(true);
        requestFocus();
    }


    public void setModel (Animatable model)
    {
        myModel = model;
    }
    
    protected void paintComponent (Graphics g)
    {
        super.paintComponent(g);
        setVisible(true);
        
        if (myModel != null)
        {
            Graphics2D pen = (Graphics2D)g;
            // make coordinate system centered and valued from [-1, 1]
            java.awt.Dimension size = getSize();
            pen.scale(size.width, size.height);
            pen.translate(0.5, 0.5);
            pen.scale(0.5, -0.5);
            myModel.paint(pen);
        }
    }
}
